package com.biostime.dingdingbox.modules.sys.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import com.biostime.dingdingbox.common.config.Global;
import com.biostime.dingdingbox.common.persistence.Page;
import com.biostime.dingdingbox.common.utils.StringUtils;
import com.biostime.dingdingbox.common.web.BaseController;
import com.biostime.dingdingbox.modules.db.entity.Myuser;
import com.biostime.dingdingbox.modules.db.service.MyUserService;
import com.biostime.dingdingbox.modules.sys.entity.Role;

/**
 * 用户表Controller
 * @author johnShen
 * @version 2015-11-12
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/myuser")
public class MyuserController extends BaseController {
	
	
	@Autowired
	private MyUserService myUserService ;

	
	@ModelAttribute
	public Myuser get(
			@RequestParam(required=false) String id,
			@RequestParam(required=false) String centerId) {
		System.out.println("----------------get--------------------");
		Myuser entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = myUserService.get(id);
		}
		if (entity == null){
			entity = new Myuser();
		}
		System.out.println("----------------get--------------------");		
		return entity;
	}
	
	@RequestMapping(value = {"list", ""})
	public String list(Myuser user, HttpServletRequest request, HttpServletResponse response, Model model) {
		//user.setCenter(UserUtils.getCenter());
		System.out.println("----------------list--------------------");
		
		Page<Myuser> page = myUserService.findPage(new Page<Myuser>(request, response), user); 
		System.out.println("pageList:"+page.getList().get(0).getUserId());
		model.addAttribute("page", page);
		System.out.println("----------------list--------------------");
		return "modules/sys/myuserList"; 
	}
	
	@RequestMapping(value = "form")
	public String form(Myuser myuser , Model model ){
		System.out.println("--------------------form-----------------");
		System.out.println("--------------------form-----------------");
		return "modules/sys/myuserForm";
		
	}
	
	@RequestMapping(value = "save")
	public String save(Myuser myuser, Model model, HttpServletRequest request, RedirectAttributes redirectAttributes) {
		System.out.println("-----------------save----------------------");
		if (!beanValidator(model, myuser)){
			return form(myuser, model);
		}
		try{
			myUserService.save(myuser);
		}catch(Exception ex){
			ex.printStackTrace();
			logger.error("myuser["+myuser.getUserId()+"]保存异常:",ex);
			
			model.addAttribute("message", ex.getMessage());
			return form(myuser, model);
		}
		addMessage(redirectAttributes, "保存成功");
		System.out.println("-----------------save----------------------");
		return "redirect:"+Global.getAdminPath()+"/sys/myuser";
	}
	
	@RequestMapping(value = "delete")
	public String delete(@RequestParam(value="ids",required=false) String idsRequest, Role role, RedirectAttributes redirectAttributes) {
		System.out.println("-----------------delete----------------------");
		//批量删除
		if(!StringUtils.isEmpty(idsRequest)){
			String[] ids = idsRequest.split(",");
			for(String id : ids){
				myUserService.delete(new Myuser(StringUtils.toInteger(id)));
			}
		}
		addMessage(redirectAttributes, "删除成功");
		System.out.println("-----------------delete----------------------");
		return "redirect:"+Global.getAdminPath()+"/sys/myuser";
	}
}